package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.domain.Floor;
import com.ruoyi.system.service.IAppmanagerService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Buildinfo;
import com.ruoyi.system.service.IBuildinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 楼栋信息Controller
 * 
 * @author yaozh
 * @date 2024-03-04
 */
@RestController
@RequestMapping("/system/buildinfo")
public class BuildinfoController extends BaseController
{
    @Autowired
    private IBuildinfoService buildinfoService;
    @Resource
    private IAppmanagerService appManagerService;

    /**
     * 查询楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:buildinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Buildinfo buildinfo,@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        if(appIds.length > 0){

        }else{
            return getDataTable(new ArrayList<>());
        }
        startPage();
        List<Buildinfo> list = buildinfoService.selectBuildinfoList(appIds,buildinfo,dbName);
        return getDataTable(list);
    }

    /**
     * 导出楼栋信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:buildinfo:export')")
    @Log(title = "楼栋信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Buildinfo buildinfo,@RequestParam String dbName)
    {
        Long[] appIds = buildinfoService.listAppIds(getLoginUser(),dbName);
        List<Buildinfo> list = new ArrayList<>();
        if(appIds.length > 0){
            list = buildinfoService.selectBuildinfoList(appIds,buildinfo,dbName);
        }
        ExcelUtil<Buildinfo> util = new ExcelUtil<Buildinfo>(Buildinfo.class);
        util.exportExcel(response, list, "楼栋信息数据");
    }

    /**
     * 获取楼栋信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:buildinfo:query')")
    @GetMapping(value = "/{buildid}")
    public AjaxResult getInfo(@PathVariable("buildid") Long buildid,@RequestParam String dbName)
    {
        return success(buildinfoService.selectBuildinfoByBuildid(buildid,dbName));
    }

    /**
     * 新增楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('system:buildinfo:add')")
    @Log(title = "楼栋信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Buildinfo buildinfo,@RequestParam String dbName)
    {
        Long appid = buildinfo.getAppid();
        if(appid == null || appid < 0){
            return error("请选择项目数据库名称");
        }
        int row = buildinfoService.insertBuildinfo(buildinfo,dbName);
        if(row == -2){
            return error("项目数据库不存在！");
        }else{
            return toAjax(row);
        }
    }

    /**
     * 修改楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('system:buildinfo:edit')")
    @Log(title = "楼栋信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Buildinfo buildinfo,@RequestParam String dbName)
    {
        return toAjax(buildinfoService.updateBuildinfo(buildinfo,dbName));
    }

    /**
     * 删除楼栋信息
     */
    @PreAuthorize("@ss.hasPermi('system:buildinfo:remove')")
    @Log(title = "楼栋信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{buildids}")
    public AjaxResult remove(@PathVariable Long[] buildids,@RequestParam String dbName)
    {
        return toAjax(buildinfoService.deleteBuildinfoByBuildids(buildids,dbName));
    }

    /**
     * 查询项目信息,通常是只有返回一个项目
     */
    @GetMapping("/appmanager")
    public AjaxResult listAllAppmanager(@RequestParam String dbName)
    {
        List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser(),dbName);
        return success(appList);
    }



}
